﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GdHotel.Data;
using GdHotel.Data.Interfaces;
using UniqueTechnologies.Attributes;

namespace GdHotel.Web.Controllers
{
    [LanguageSupport]
    [Authorize(Roles = "Admin")]
    public class ManageEmailsController : BaseController
    {
        public IEmailsRepository EmailsRepository { get; set; }
        public override ILanguagesRepository LanguagesRepository { get; set; }

        public ManageEmailsController(IEmailsRepository emailsRepository, ILanguagesRepository languagesRepository)
        {
            EmailsRepository = emailsRepository;
            LanguagesRepository = languagesRepository;
        }

        //
        // GET: /Emails/

        public ViewResult Index()
        {
            var emails = EmailsRepository.FindAll();
            return View(emails);
        }

        //
        // GET: /Emails/Details/5

        public ViewResult Details(Int32 id)
        {
            var email = EmailsRepository.GetById(id);
            return View(email);
        }

        //
        // GET: /Emails/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Emails/Create

        [HttpPost]
        public ActionResult Create(Email email)
        {
            if (ModelState.IsValid)
            {
                EmailsRepository.Insert(email);
                EmailsRepository.Save();
                return RedirectToAction("Index");
            }

            return View(email);
        }

        //
        // GET: /Emails/Edit/5

        public ActionResult Edit(int id)
        {
            var email = EmailsRepository.GetById(id);
            return View(email);
        }

        //
        // POST: /Emails/Edit/5

        [HttpPost]
        public ActionResult Edit(Email email)
        {
            var dbEmail = EmailsRepository.GetById(email.Id);
            if (ModelState.IsValid)
            {
                UpdateModel(dbEmail);
                EmailsRepository.Save();
                return RedirectToAction("Index");
            }
            return View(email);
        }

        //
        // GET: /Emails/Delete/5

        public ActionResult Delete(int id)
        {
            var email = EmailsRepository.GetById(id);
            return View(email);
        }

        //
        // POST: /Emails/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var email = EmailsRepository.GetById(id);
            EmailsRepository.Delete(email);
            EmailsRepository.Save();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            EmailsRepository.Dispose();
            base.Dispose(disposing);
        }
    }
}